﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Threading;
using System.Security.Permissions;

namespace 时间转换
{
    [assembly: SecurityPermission(SecurityAction.RequestMinimum, ControlThread = true)]
    class Program
    {    //密码 CE8VZ8RW2hS9
        const string dataFmt = "{0,-12}{1,8}       {2,-18}{3,11}";
        static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-cn");
            Thread.CurrentThread.CurrentCulture.Calendar.TwoDigitYearMax = 2099;
            string result = new DateTime(2099, 2, 13).ToString("yy-MM-dd");

            DateTime time;
            DateTime.TryParse(result, out time);
           
            Console.WriteLine(time.ToString("yyyy-MM-dd"));
            //result2?
           // Console.WriteLine(DateTime .Now.ToString("yy-MM-dd  G\\MT zzz"));
            //Console.WriteLine(DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo.LongTimePattern ,DateTimeFormatInfo.InvariantInfo));
            //Console.WriteLine(DateTimeFormatInfo.InvariantInfo.LongTimePattern);
            //Console.WriteLine(DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo.LongDatePattern , DateTimeFormatInfo.InvariantInfo));
            //Console.WriteLine(DateTimeFormatInfo.InvariantInfo.LongDatePattern);
            //Console.WriteLine(DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo.ShortDatePattern , DateTimeFormatInfo.InvariantInfo));
            //Console.WriteLine(DateTimeFormatInfo.InvariantInfo.ShortDatePattern);
            //Console.WriteLine(DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo.ShortTimePattern, DateTimeFormatInfo.InvariantInfo));
            //Console.WriteLine(DateTimeFormatInfo.InvariantInfo.ShortTimePattern);
           
            #region 修改当前进程的Calendar.TwoDigitYearMax
            //DateTimeConvert.ShortTimeToLongforError();
            ////DateTimeConvert.ShortTimeToLong();
            #endregion
            #region  DateTime
            
            #region hh(12小时)和HH(24小时)区别
            //DateTime time = DateTime.Now.AddHours(4);
            //Console.WriteLine(time.ToString("yyyyMMddHHmmss"));
            //Console.WriteLine(time.ToString("yyyyMMddhhmmss"));
            #endregion
            #region http://www.cnblogs.com/yangcaogui/archive/2012/03/01/2372024.html  年份的两位和四位转换问题  Calendar.TwoDigitYearMax=2029
            // Console.WriteLine("无格式化：" + DateTime.Now.AddMonths(1010).ToString());
            // Console.WriteLine(string.Format("格式化了 → {0:yy-MM-dd}", DateTime.Now.AddMonths(1010)));
            // string str = Convert.ToDateTime(string.Format("{0:yy-MM-dd}", DateTime.Now.AddMonths(1010))).ToString();
            // Console.WriteLine("格式化了，但又转换了：" + str);
            //// Calendar myCal = CultureInfo.InvariantCulture.Calendar;

            #endregion
            #region Now(本地时区时间)与UTCNow（0时区时间）
            //long trick = DateTime.Now.Ticks;
            //long utcTrick = DateTime.UtcNow.Ticks;
            //Console.WriteLine(trick);
            //Console.WriteLine(utcTrick);
            //Console.WriteLine(DateTime.Now.ToString());
            //Console.WriteLine(DateTime.UtcNow.ToString());
            #endregion
            #region DateTimeKind
            //当我们通过构造函数创建一个DateTime对象的时候，Kind默认为DateTimeKind.Unspecified。 
            //DateTime.Now表示当前系统时间，Kind属性值为DateTimeKind.Local，所以DateTime.Now应该是DateTime.LocalNow； 
            //而DateTime.UtcNow返回以UTC表示的当前时间，毫无疑问，Kind属性自然是DateTimeKind.Utc; 
            //DateTime.MinValue和DateTime.MaxValue表示的DateTime所能表示的最大范围，它们的Kind属性为DateTimeKind.Unspecified

            //DateTime endOfTheWorld = new DateTime(2012, 12, 21);
            //Console.WriteLine("endOfTheWorld.Kind = {0}", endOfTheWorld.Kind);
            //Console.WriteLine("DateTime.SpecifyKind(endOfTheWorld, DateTimeKind.Utc).Kind = {0}", DateTime.SpecifyKind(endOfTheWorld, DateTimeKind.Utc).Kind);
            //Console.WriteLine("endOfTheWorld.Kind = {0}", endOfTheWorld.Kind);
            //Console.WriteLine("DateTime.Now.Kind = {0}", DateTime.Now.Kind);
            //Console.WriteLine("DateTime.UtcNow.Kind = {0}", DateTime.UtcNow.Kind);
            //Console.WriteLine("DateTime.MinValue.Kind = {0}", DateTime.MinValue.Kind);
            //Console.WriteLine("DateTime.MaxValue.Kind = {0}", DateTime.MaxValue.Kind);
            #endregion
            #region DateTime的对等性问题(时间比较和时区无关)
            //DateTime endOfTheWorld1 = new DateTime(2012, 12, 21, 0, 0, 0, DateTimeKind.Local);
            //DateTime endOfTheWorld2 = new DateTime(2012, 12, 21, 0, 0, 0, DateTimeKind.Unspecified);
            //DateTime endOfTheWorld3 = new DateTime(2012, 12, 21, 0, 0, 0, DateTimeKind.Utc);
            //Console.WriteLine("endOfTheWorld1 == endOfTheWorld2 = {0}", endOfTheWorld1 == endOfTheWorld2);
            //Console.WriteLine("endOfTheWorld2 == endOfTheWorld3 = {0}", endOfTheWorld2 == endOfTheWorld3);
            ////tricks 也不包含时区
            //Console.WriteLine(endOfTheWorld1.Ticks);
            //Console.WriteLine(endOfTheWorld2.Ticks);
            //Console.WriteLine(endOfTheWorld3.Ticks);
            //Console.WriteLine(endOfTheWorld3.ToString());

            //Console.WriteLine(endOfTheWorld1.Ticks);
            //Console.WriteLine(endOfTheWorld1.ToUniversalTime().Ticks);
            #endregion
            #region  时间转化是创建一个新的对象
            //实际上指的就是将一个具有某种DateTimeKind的DateTime对象转化成具有另外一种DateTimeKind的DateTime对象，并且确保两个DateTime对象对象表示相同的时间点。
            #region 通过DateTime类型的ToLocalTime和ToUniversalTime方法实现UTC和Local的转换  Unspecified比较特殊。
            ////DataTimeKind.Unspecifed时间分别向其他两种DateTimeKind时间的转换问题。从下面的输出我们可以看出，
            ////当DateTimeKind.Unspecifed时间向DateTimeKind.Local转换的时候，实际上是当成DateTimeKind.Utc时间；
            ////而向DateTimeKind.Utc转换的时候，则当成是DateTimeKind.Local。顺便补充一下：不论被转换的时间属于怎么的DateTimeKind，
            ////调用ToLocalTime和ToUniversalTime方法的返回的时间的Kind属性总是DateTimeKind.Local和DateTimeKind.Utc，
            ////两者之间的转换并不只是年月日和时分秒的改变。
            //DateTime endOfTheWorld1 = new DateTime(2012, 12, 21, 0, 0, 0, DateTimeKind.Local);
            //DateTime endOfTheWorld2 = new DateTime(2012, 12, 21, 0, 0, 0, DateTimeKind.Unspecified);
            //DateTime endOfTheWorld3 = new DateTime(2012, 12, 21, 0, 0, 0, DateTimeKind.Utc); 
            //Console.WriteLine("endOfTheWorld1.ToLocalTime() = {0}",endOfTheWorld1.ToLocalTime());
            //Console.WriteLine("endOfTheWorld2.ToLocalTime() = {0}", endOfTheWorld2.ToLocalTime());
            //Console.WriteLine("endOfTheWorld3.ToLocalTime() = {0}\n", endOfTheWorld3.ToLocalTime()); 
            //Console.WriteLine("endOfTheWorld1.ToUniversalTime() = {0}", endOfTheWorld1.ToUniversalTime());
            //Console.WriteLine("endOfTheWorld2.ToUniversalTime() = {0}", endOfTheWorld2.ToUniversalTime());
            //Console.WriteLine("endOfTheWorld3.ToUniversalTime() = {0}", endOfTheWorld3.ToUniversalTime());
            #endregion

            #endregion
            #region TimeZoneInfo 任意时区间的转换 TimeZoneInfo 类的实例是不可变的。一旦实例化对象后，就无法修改其值。
            // DateTime ConvertTime(DateTime dateTime, TimeZoneInfo destinationTimeZone);
            //DateTime ConvertTime(DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone);
            //DateTime ConvertTimeFromUtc(DateTime dateTime, TimeZoneInfo destinationTimeZone);
            //DateTime ConvertTimeToUtc(DateTime dateTime, TimeZoneInfo sourceTimeZone);
            #region TimeZoneInfo.TransitionTime  自定义夏令时

            //////TimeZoneInfo.TransitionTime CreateFixedDateRule(DateTime timeOfDay, int month, int day);

            //////TimeZoneInfo.TransitionTime CreateFloatingDateRule(DateTime timeOfDay, int month, int week, DayOfWeek dayOfWeek);
            ////TimeZoneInfo imaginaryTZ;
            //TimeSpan delta = new TimeSpan(1, 0, 0);
            ////不固定时间
            ////transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 3, 5, DayOfWeek.Sunday);
            ////transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 3, 0, 0), 10, 4, DayOfWeek.Sunday);
            //TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFixedDateRule(new DateTime(1, 1, 1, 17, 0, 0), 3, 15);
            //TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFixedDateRule(new DateTime(1, 1, 1, 23, 0, 0), 11, 25);

            //List<TimeZoneInfo.AdjustmentRule> adjustmentList = new List<TimeZoneInfo.AdjustmentRule>();
            //TimeZoneInfo.AdjustmentRule adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(2012, 1, 1), new DateTime(2012, 12, 31), delta, start, end);
            //adjustmentList.Add(adjustment);
            //TimeZoneInfo imaginaryTZ = TimeZoneInfo.CreateCustomTimeZone("MyTimeZOne", new TimeSpan(8, 0, 0),
            //                 "(GMT+08:00) Fictitious Time", "Fictitious Standard Time",
            //                 "Fictitious Daylight Time", adjustmentList.ToArray());
            DateTime mytime = TimeZoneInfo.ConvertTime(new DateTime(2012, 4, 1, 17, 33, 0, DateTimeKind.Local), DateTimeConvert.CreateFixedTimeRuleTimeZone());
            Console.WriteLine(mytime.ToString());
            #endregion
            #region 自定义规则时区  TimeZoneInfo.AdjustmentRule
            //TimeZoneInfo.AdjustmentRule CreateAdjustmentRule(DateTime dateStart, DateTime dateEnd, TimeSpan daylightDelta, TimeZoneInfo.TransitionTime daylightTransitionStart, TimeZoneInfo.TransitionTime daylightTransitionEnd)
            //TimeZoneInfo myZone=TimeZoneInfo .AdjustmentRule .CreateAdjustmentRule (DateTime.Now ,DateTime .Now.AddYears(1),
            //TimeSpan span = TimeSpan.MinValue;
            //TimeSpan span2 = TimeSpan.MaxValue;
            //Console.WriteLine(span.ToString());
            //Console .WriteLine ()
            #endregion
            #region 转换带有夏令时的时区
            //DateTime time = DateTime.Now;
            //TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById("Central America Standard Time");
            //DateTime desTime = TimeZoneInfo.ConvertTime(time, zone);
            //Console.WriteLine(desTime.ToString());
            //Console.WriteLine(zone.BaseUtcOffset);
            //Console.WriteLine(zone.SupportsDaylightSavingTime);
            #endregion
            #region 得到SystemTimezone的信息
            //foreach (var timeZone in TimeZoneInfo.GetSystemTimeZones())
            //{
            //    Console.WriteLine("{0,-12}{1,8}", timeZone.DisplayName,  timeZone.SupportsDaylightSavingTime.ToString());
            //}
            
            #endregion
            #endregion

            #endregion


           
            #region TimeSpan 可以为正负  复合格式化
            //ShowTimeSpanProperties(new TimeSpan(-10000000000000000));
            #endregion
            //DateTimeConvert.DateTimeDefaultString();

        }

        private static void WriteLine(string p)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// for TimeSpan
        /// </summary>
        /// <param name="interval"></param>
        static void ShowTimeSpanProperties(TimeSpan interval)
        {
            Console.WriteLine("{0,21}", interval);
            Console.WriteLine(dataFmt, "Days", interval.Days,
                "TotalDays", interval.TotalDays);
            Console.WriteLine(dataFmt, "Hours", interval.Hours,
                "TotalHours", interval.TotalHours);
            Console.WriteLine(dataFmt, "Minutes", interval.Minutes,
                "TotalMinutes", interval.TotalMinutes);
            Console.WriteLine(dataFmt, "Seconds", interval.Seconds,
                "TotalSeconds", interval.TotalSeconds);
            Console.WriteLine(dataFmt, "Milliseconds",
                interval.Milliseconds, "TotalMilliseconds",
                interval.TotalMilliseconds);
            Console.WriteLine(dataFmt, null, null,
                "Ticks", interval.Ticks);
        }

    }

}
